package adapter.springmvc;

import java.util.ArrayList;
import java.util.List;

public class DispatchServlet {


    public static List<HandleAdapter> handleAdapters = new ArrayList<HandleAdapter>();


    public DispatchServlet(){
        handleAdapters.add(new SimpleHandleAdapter());
        handleAdapters.add(new HttpHandleAdapter());
        handleAdapters.add(new AnnotationHandleAdapter());
    }



    public void doDispatch(){

        //AnnotationController controller = new AnnotationController();
        SimpleController controller = new SimpleController();
        HandleAdapter adapter = getHandler(controller);
        adapter.handle(controller);


    }

    private HandleAdapter getHandler(final Controller controller){
        HandleAdapter handleAdapter = handleAdapters.stream().filter(item -> {
            return item.supports(controller);
        }).findFirst().orElse(null);
        return handleAdapter;
    }


    public static void main(String[] args) {
        new DispatchServlet().doDispatch();
    }
}
